package com.jiai.cloud.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TPageModule;
import com.jiai.cloud.feign.PageModuleAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by chenyu on 2017/5/24.
 */
@RestController
@RequestMapping("/api/v1/pageModule")
public class PageModuleController {

    @Autowired
    PageModuleAPI pageModuleAPI;

    /**
     * 直接调用feign，feign会去调用eurekaService
     * */
    //增
    @RequestMapping(value="/addPage", method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object saveOrderdetails(@RequestBody TPageModule tPageModule){
        pageModuleAPI.addPageModule(tPageModule);
        return RestResultGenerator.genSuccessResult("");
    }
    //删
    @RequestMapping(value="/delete/{id}", method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object deleteById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            pageModuleAPI.delete(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗",e.getMessage());
        }
    }
    //ID查询
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            pageModuleAPI.selectById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }
    //查询所有的
    @RequestMapping(value = "/findByAll/{page}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object findGoodsByAll(@PathVariable("page") Integer page1) throws ServiceException {
        try{
            Page<TPageModule> page = new Page<>(page1, 20);
            pageModuleAPI.selectPage(page1);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        }catch (Exception e){
            throw new ServiceException("查询失败",e.getMessage());
        }
    }


    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object updateOrInsert(@RequestBody TPageModule tPageModule)throws ServiceException {
        try {
            pageModuleAPI.edit(tPageModule);
            return RestResultGenerator.genSuccessResult("");
        } catch (Exception e) {
            throw new ServiceException("编辑失敗", e.getMessage());
        }
    }

}
